package user

import (
	"context"
	"time"

	"go-weixin/app/usercenter/api/internal/svc"
	"go-weixin/app/usercenter/api/internal/types"
	"go-weixin/app/usercenter/rpc/pb/proto/usercenter"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserGetRoleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserGetRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserGetRoleLogic {
	return &UserGetRoleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserGetRoleLogic) UserGetRole(req *types.UserGetRoleReq) (resp *types.UserGetRoleResp, err error) {
	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()
	res, err := l.svcCtx.UserRpc.UserGetRole(ctx, &usercenter.UserGetRoleReq{
		Id: req.UserId,
	})
	if err != nil {
		return nil, err
	}
	var roleList []types.Role
	if len(res.RoleList) > 0 {
		for _, role := range res.RoleList {
			roleList = append(roleList, types.Role{
				Id:       role.Id,
				Name:     role.Name,
				ParentId: role.ParentId,
				Weight:   role.Weight,
				CreateAt: role.CreateAt,
				UpdateAt: role.UpdateAt,
			})
		}
	}
	resp = new(types.UserGetRoleResp)
	resp.RoleList = roleList
	return
}
